home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2439 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
  2. From: Dan Pop <danpop@mail.cern.ch>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: quick decision: is n a power of 2?
  5. Date: Sun, 21 Jan 1996 16:09:28 +0100
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <9601211509.AA01185@dxmint.cern.ch>
  8. References: <4dpd94$c25@fountain.mindlink.net> <4dsaco$a39@newsbf02.news.aol.com> <9601211103.AA19325@dxmint.cern.ch> <31022d1e.215811776@nntp.ix.netcom.com>
  9. X-NNTP-Posting-Host: hpl3sn03.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11. X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
  12.  
  13. miker3@ix.netcom.com (Mike Rubenstein) writes:
  14.  
  15. >> babycox@aol.com (BabyCox) writes:
  16. >> 
  17. >> >Here is a revision, this should work (but that's what I said last time)
  18. >> 
  19. >> You're a little bit closer this time :-)
  20. >> 
  21. >> >Boolean isPowerOfTwo(long x)
  22. >> >{
  23. >> >  long tst;
  24. >> >  for(tst=1<<31;tst!=0;tst>>=1)
  25. >> >    if (x==tst) return true;
  26. >> >  
  27. >> >  return false;
  28. >> >}
  29. >> 
  30. >There's also the problem that it will fail if long is more than 32
  31. >bits and x is greater than pow(2, 32).
  32.                       ^
  33.                    or equal
  34.  
  35. The original poster asked for a solution for 32-bit unsigned long's so
  36. x cannot be greater than pow(2, 32) (or even equal).
  37.  
  38. The nice thing is that all the fast (i.e. non-iterative) solutions
  39. don't need to know the size of the argument.  When implemented as
  40. macros, their argument can have any unsigned type.
  41.  
  42. Dan
  43. -- 
  44. Dan Pop
  45. CERN, CN Division
  46. Email: danpop@mail.cern.ch 
  47. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  48.